// packageA/pages/index/baojiaNavDetal/baojiaNavDetail.js
import * as echarts from '../../../ec-canvas/echarts';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentQSNavIndex: 0,
    navType: true,
    currenTuijianIndex: 0,
    currentMID: 81,
    CJshow: false,
    echartsShow: true,
    CaiJiUserTips: '',
    CJcomType: ['废金属', '再生塑料', '废纸', '铜厂、铜材厂', '调剂商、贸易商', '钢厂、钢铁冶金'],
    CJcomTypeIndex: 0,
    CaiJiUserTips: '',
    liuyantext: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    wx.showLoading({
      title: '加载中',
    })
    // console.log(options, '报价二级页面跳转参数')
    let that = this
    if (options.ClassIndex == '1.5' || options.ClassIndex == '1.~5678' || options.ClassIndex == '2.') { //圆的
      that.setData({
        navType: true
      })
    } else {
      that.setData({
        navType: false
      })
    }
    await that.queryBaojiaView(options.ClassIndex)
    // that.queryisCJ()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 第一个数据表开始
  async getData(index) {
    // console.log('shujub初始化')
    let that = this,
      QS = this.data.moduleQushi.MData,
      list = QS[index].QSData
    this.setData({
      QSDataList: list.DataList,
      QSechartsX: list.Date,
      QSechartsLine: [{
        data: list.Max,
        type: 'line',
        smooth: true,
        // symbol: 'none', //取消折点圆圈
        // areaStyle: {
        //   opacity: 0.4
        // }
      },
      {
        data: list.Min,
        type: 'line',
        smooth: true,
        // symbol: 'none', //取消折点圆圈
        // areaStyle: {
        //   opacity: 0.4
        // }
      }
      ]
    }, () => {
      that.init_echarts();
    })
    // await utilrequest.getUtilsThings({
    //   sqlname: 'reportquerysum',
    //   parameter: `${param.deptid},${param.brandid},${param.brandid},${param.deptid},${param.patch},${param.areaCode}`,
    // }).then(res => {
    //   // console.log(res, '离线统计')
    //   let a1 = res
    //   that.setData({

    //   },()=>{
    //   })
    // })
  },
  //初始化图表--------------
  init_echarts: function () {
    // console.log('init')
    let that = this
    this.echartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const Chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      // Chart.on('click', function (params) {
      //   console.log(params, '高点');
      // });
      Chart.setOption(that.getOption());
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return Chart;

    });
  },
  getOption: function () {
    // console.log('getOption')
    let that = this
    // 指定图表的配置项和数据
    var option = {
      grid: {
        top: 10,
        left: 50,
        height: 115,
        // width:300,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: that.data.QSechartsX
      },
      yAxis: {
        type: 'value'
      },
      // legend: {
      //   top:0,
      //   left:0,
      // },
      tooltip: {
        show: true,
        trigger: 'axis',
        confine: true
      },
      series: that.data.QSechartsLine
    };
    return option;
  },
  // 第一个数据表结束============

  // 二级页面数据
  queryBaojiaView(e) {
    let that = this,
      moduleNav,
      modulePic,
      moduleFenxi,
      moduleQushi,
      moduletuijian,
      modulePicBottom
    app.getDaoPost('api/FeijiuApp/Feijiu_BJ_Get_ClassIndex', {
      ClassIndex: e
    }, r => {
      // console.log(r, '二级页面数据')
      if (r.code == 200) {
        r.data.forEach((item, index) => {
          if (item.MID == 2) { //nav
            moduleNav = item
          } else if (item.MID == 4) { //Pic
            modulePic = item
          } else if (item.MID == 5) { //报价分析
            moduleFenxi = item
          } else if (item.MID == 3) { //实时趋势
            moduleQushi = item
          } else if (item.MID == 8) { //商机推荐
            moduletuijian = item
            that.setData({
              tuijianMUpTime: item.MData[0].MUpTime
            })
          } else if (item.MID == 10) { //Pic
            modulePicBottom = item
          }
        })

      }
      that.setData({
        moduleNav,
        modulePic,
        moduleFenxi,
        moduleQushi,
        moduletuijian,
        modulePicBottom
      }, async () => {
        that.echartsComponnet = that.selectComponent('#mychart');
        await that.getData(0); //获取数据1
      })
      wx.hideLoading({
        success: (res) => { },
      })
    })
  },
  // 商机推荐标题点击
  cilckSahngjiTuijian(e) {
    let index = e.currentTarget.dataset.index,
      id = e.currentTarget.dataset.id,
      that = this,
      arr = that.data.moduletuijian
    that.setData({
      currenTuijianIndex: index,
      currentMID: id,
      tuijianMUpTime: arr.MData[index].MUpTime
    })
  },
  clickQSNav(e) {
    // console.log(e,'')
    let index = e.currentTarget.dataset.index,
      QS = this.data.moduleQushi.MData,
      list = QS[index].QSData,
      that = this
    this.setData({
      currentQSNavIndex: index,
      QSechartsX: list.Date,
      QSechartsLine: [{
        data: list.Max,
        type: 'line',
        smooth: true,
        // symbol: 'none', //取消折点圆圈
        // areaStyle: {
        //   opacity: 0.4
        // }
      },
      {
        data: list.Min,
        type: 'line',
        smooth: true,
        // symbol: 'none', //取消折点圆圈
        // areaStyle: {
        //   opacity: 0.4
        // }
      }
      ],
      QSDataList: list.DataList,
    }, async () => {
      that.echartsComponnet = that.selectComponent('#mychart');
      await that.getData(index); //获取数据1
    })
  },
  navJumpDetail(e) {
    wx.showToast({
      title: '请前往Feijiu网APP查看详情',
      icon: 'none'
    })
  },
  onClickMOre(e) {
    if (this.data.currenTuijianIndex == 1) {
      wx.navigateTo({
        url: '/packageA/pages/zichanchuzhi/index',
      })
    } else if (this.data.currenTuijianIndex == 0) {
      wx.switchTab({
        url: '/pages/zhaobiaogonggao/index',
      })
    } else if (this.data.currenTuijianIndex == 2 || this.data.currenTuijianIndex == 3) {
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/index',
      })
    }
  },
  zichanchuzhiDetail(e) { //资产处置详情跳转
    // console.log(e, '===')
    let id = e.currentTarget.dataset.id,
      uuid = e.currentTarget.dataset.uuid

    if (this.data.currenTuijianIndex == 1) {
      wx.navigateTo({
        url: '/packageA/pages/zichanchuzhi/detailed?gqid=' + id,
      })
    } else if (this.data.currenTuijianIndex == 0) {
      wx.navigateTo({
        url: '/pages/zhaobiaogonggao/detailed?uuid=' + uuid,
      })
    } else if (this.data.currenTuijianIndex == 2 || this.data.currenTuijianIndex == 3) {
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/detailed?gqid=' + id,
      })
    }


  },
  async bottomPic(e) {
    // console.log(e, '底部图片')
    let id = e.currentTarget.dataset.id
    if (id == 1) { //我要纠错
      wx.navigateTo({
        url: '/packageA/pages/index/baojiamyjiucuo/baojiamyjiucuo',
      })
    } else if (id == 2) { //我要发布
      await this.queryisCJ()
      // if (this.data.isCaiJiUser == 1) {
      //   wx.navigateTo({
      //     url: '/packageA/pages/index/baojiaFaubu/baojiaFabu',
      //   })
      // }
    }
  },
  // 申请成为采集单位
  applyCJ() {
    let that = this
    // LoginUID 登录用户编号     CompanyType 公司类型名称    Message 留言   CompanyName 公司名  LinkMan 联系人姓名   LinkTel 联系人电话
    if (!this.data.Linkman) {
      wx.showToast({
        title: '请填写联系人',
        icon: 'none'
      })
      return
    } else if (!this.data.Mobile) {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none'
      })
      return
    } else if (this.data.Mobile.length < 11) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }
    let input = {
      LoginUID: wx.getStorageSync('FjLoginLog').UID,
      CompanyType: this.data.CJcomType[this.data.CJcomTypeIndex],
      Message: this.data.liuyantext,
      CompanyName: this.data.ComName ? this.data.ComName : '',
      LinkMan: this.data.Linkman,
      LinkTel: this.data.Mobile
    }
    app.getDaoPost('api/FeijiuApp/Feijiu_BJ_apply_CollectUser', input, r => {
      // console.log(r, '采集单位申请')
      if (r.code == 200) {
        wx.showToast({
          title: r.data,
        })
        that.setData({
          CJshow: false,
          echartsShow: true,
        })
        that.echartsComponnet = that.selectComponent('#mychart');
        that.getData(0); //获取数据1
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        })
      }
    })
  },
  //查询是不是采集单位 有没有登录
  async queryisCJ() {
    let that = this
    if (app.checkFjLogin()) {
      var FjLoginLog = wx.getStorageSync('FjLoginLog');
      var api_input = {
        uid: FjLoginLog.UID,
        upwd: FjLoginLog.UPwd
      }
      //采集单位
      await app.getDaoPost('api/FeijiuApp/Post_FJ_UserInfoByUID', api_input, function (r) {
        var udata = JSON.parse(app.aesDec(r.data));
        // console.log(udata, '===采集单位');
        that.setData({
          ComName: udata.Uinfo[0].ComName,
          Linkman: udata.Uinfo[0].Linkman,
          Mobile: udata.Uinfo[0].Mobile,
          isCaiJiUser: udata.isCaiJiUser,
          CaiJiUserTips: udata.CaiJiUserTips
        })
        if (udata.isCaiJiUser == 0) { //1 是 0不是没有提交申请 2不是提交过
          that.setData({
            CJshow: true,
            echartsShow: false,
          })
        } else if (udata.isCaiJiUser == 2) { //
          wx.showToast({
            title: udata.CaiJiUserTips,
            icon: 'none'
          })

        } else if (udata.isCaiJiUser == 1) {
          wx.navigateTo({
            // url: '/packageA/pages/index/baojiaFaubu/baojiaFabu',
            url: '/packageB/pages/mybaojia/index'
          })
        }

      })
    } else {
      wx.navigateTo({
        url: '/pages/login/index'
      });
    }
  },
  onCloseCJ() {
    let that = this
    this.setData({
      CJshow: false,
      echartsShow: true,
    })
    that.echartsComponnet = that.selectComponent('#mychart');
    that.getData(0); //获取数据1
  },
  selectComType(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      CJcomTypeIndex: index
    })
  },
  GJcomInput(e) {
    this.setData({
      ComName: e.detail.value
    })
  },
  GJUserPhoneInput(e) {
    this.setData({
      Mobile: e.detail.value
    })
  },
  GJUserNameInput(e) {
    this.setData({
      Linkman: e.detail.value
    })
  },
  liuyanInput(e) {
    this.setData({
      liuyantext: e.detail.value
    })
  },
  callPhone(e) {
    let ph = e.currentTarget.dataset.ph
    wx.makePhoneCall({
      phoneNumber: ph //仅为示例，并非真实的电话号码
    })
  },
  async navJumpDetail(e) {
    // console.log(e, '跳转二级')
    let ClassIndex = e.currentTarget.dataset.classindex,
      type = e.currentTarget.dataset.type
    if (type == 2) { //发布报价
      await this.queryisCJ()
    } else if (type == 3) {
      wx.switchTab({
        url: '/pages/dingyue/index',
      })
    } else if (type == 8) { //我要纠错
      wx.navigateTo({
        url: '/packageA/pages/index/baojiamyjiucuo/baojiamyjiucuo',
      })
    } else {
      wx.showToast({
        title: '请前往Feijiu网APP查看详情',
        icon: 'none'
      })
    }
  },

})